<?php
/*
Plugin Name: LightPress
Plugin URI: http://lightpress.org/
Description: Replaces the WordPress frontend with a faster, templated frontend. Once enabled, go to the "LightPress" menu to customize your installation.
Version: 2.0.0
Author: Ludovico Magnocavallo, Jerome Lavigne
Author URI: http://lightpress.org/
*/

// use WordPress localization-functionality in the Administration __(   ,'lp')
load_plugin_textdomain('lp', $path = 'wp-content/plugins/lightpress');

// sync wp_version
if (!get_option('lp_opt_wp_version') || get_option('lp_opt_wp_version') != $wp_version)
    update_option('lp_opt_wp_version', $wp_version);

/***** Add LP Admin Menus *****/
function lpadmin_extend_menu() {
    add_menu_page(__('LightPress Administration','lp'), __('LightPress','lp'), 'manage_options', 'lightpress/wp-plugin/main.php');
    
    if (get_option('lp_opt_basedir') || (isset($_POST['lp_install']) && $_POST['lp_install'] == 'stage3')) {
        add_submenu_page('lightpress/wp-plugin/main.php', __('LightPress General Options','lp'), __('General','lp'), 'manage_options', 'lightpress/wp-plugin/main.php');
        add_submenu_page('lightpress/wp-plugin/main.php', __('LightPress Options','lp'), __('Options','lp'), 'manage_options', 'lightpress/wp-plugin/options.php');
        add_submenu_page('lightpress/wp-plugin/main.php', __('LightPress Plugins','lp'), __('Plugins','lp'), 'manage_options', 'lightpress/wp-plugin/plugins.php');
        add_submenu_page('lightpress/wp-plugin/main.php', __('LightPress Themes','lp'), __('Themes','lp'), 'manage_options', 'lightpress/wp-plugin/themes.php');
        add_submenu_page('lightpress/wp-plugin/main.php', __('LightPress Configuration File','lp'), __('Configuration File','lp'), 'manage_options', 'lightpress/wp-plugin/config.php');
        $permalink_structure = get_option('permalink_structure');
        if (!empty($permalink_structure))
            add_submenu_page('lightpress/wp-plugin/main.php', __('LightPress Rewrite Rules','lp'), __('Rewrite Rules','lp'), 'manage_options', 'lightpress/wp-plugin/mod_rewrite.php');
        add_submenu_page('lightpress/wp-plugin/main.php', __('LightPress Tags','lp'), __('Tags','lp'), '__(manage_options', 'lightpress/wp-plugin/managetags.php');
        add_submenu_page('lightpress/wp-plugin/main.php', __('LightPress Preformatter','lp'), __('Preformatter','lp'), 'manage_options', 'lightpress/wp-plugin/preformatter.php');
        add_submenu_page('lightpress/wp-plugin/main.php', __('LightPress Cache','lp'), __('Cache','lp'), 'manage_options', 'lightpress/wp-plugin/cache.php');

        // trim WP menus to exclude screens we don't need
        if (get_option('lp_opt_install_type') == 'root') {
            unset($GLOBALS['menu'][25]);                //remove 'Presentation'
            unset($GLOBALS['submenu']['themes.php']);
            unset($GLOBALS['admin_page_hooks']['themes.php']);
            // writing controls the textarea size for posts, etc.
            //unset($GLOBALS['submenu']['options-general.php'][10]);    //remove 'Options - Writing'
            //unset($GLOBALS['submenu']['options-general.php'][25]);    //remove 'Options - Permalinks'
        }
        
    }
    
}

add_action('admin_menu', 'lpadmin_extend_menu');

function lpadmin_extra_css() {
    echo '<style type="text/css">
    h3 { margin: 0px; margin-top: 12px; margin-bottom: 6px; }
    caption { font-weight: bold; text-align: center; }
    label { font-weight: bold; }
    small { font-size: 0.9em; font-style: italic; color: #666; }
    code { color: #333366; font-style: italic; }
    code.opt { font-weight: bold; font-style: normal; }
    .error { color: #993333; }
    .warning { color: #cc33cc; }
    .field_warning { border: solid 1px #993333; }
    .lp_paragraph { line-height: 1.5em; text-align: justify; }
    div.theme { }
    div.theme img { float: left; border: solid 1px #ccc; padding: 3px; margin-right: 9px; }
    pre {background:#000;color:slategray}
    </style>
    <link rel="stylesheet" type="text/css" href="'.get_option('lp_opt_url').'/wp-content/plugins/lightpress/js/star-light.css" />';
}

add_action('admin_head', 'lpadmin_extra_css');

/***** Add actions for the options controlled by WP ****/

// the WP filters/actions system is totally braindead, why is it impossible
// to retrieve the name of the 'tag' from the called filter/action? Why
// is the update_option action different for each option, and does not
// receive the option name as its first argument?

if ($basedir = get_option('lp_opt_basedir')) {
    require_once implode(DIRECTORY_SEPARATOR, array($basedir, 'wp-plugin', 'LightPressOptions.php'));
    $lp_opts = new LightPressOptions();
    $lp_opts_names = array_values($lp_opts->_wpoption);
    $lp_opts_names = array_merge($lp_opts_names, array('blog_charset', 'permalink_structure', 'category_base', 'rss_use_excerpt'));
    foreach ($lp_opts_names as $opt) {
        $action_name = create_function('$old, $new', "lp_sync_option('$opt', \$old, \$new);");
        add_action("update_option_$opt", $action_name, 10, 2);
    }
}

function lp_sync_option($opt, $old, $new) {
    
    global $lp_opts;
    $map = array_flip($lp_opts->_wpoption);
    
    switch ($opt) {
        case 'permalink_structure':
            foreach (array('post_prefix', 'archives_prefix') as $opt)
                $lp_opts->updateOption("lp_opt_$opt", $lp_opts->getDefault($opt));
            break;
        case 'rss_use_excerpt':
            if ($new != $old)
                $lp_opts->updateOption("lp_opt_rss_use_excerpt", $new);
            break;
        case 'category_base':
            $new = $lp_opts->removeWPslashes($new);
        default:
            if (isset($map[$opt]))
                $lp_opts->updateOption("lp_opt_{$map[$opt]}", $new);
            else
                $lp_opts->updateOption("lp_opt_$opt", $new);
    }
}

/***** Add LP Rewrite rule filters *****/
function lpadmin_rewrite_rules($rules) {
    require_once 'wp-plugin/LightPressModRewrite.php';
    if (!isset($lightpress_rewrite_helper))
        $lightpress_rewrite_helper =& new LightPressModRewrite();
    
    if ($lightpress_rewrite_helper->basedir) {
        
        // check to see if we can override the .htaccess file in the WP root
        if (get_option('lp_opt_install_type') == 'root') {
            return $lightpress_rewrite_helper->buildHtaccess();
        } else {
            return $rules . "\n# " . __('LightPress is in testing-mode, please check the LightPress admin console\n# for LP .htaccess rules', 'lp') . "\n";
        }
    } else {
        return $rules;
    }
}
add_filter('mod_rewrite_rules', 'lpadmin_rewrite_rules');

/***** LP Tagging *****/
require_once 'wp-plugin/LightPressTagging.php';
$lightpress_tag_helper =& new LightPressTagging();


/***** LP Cache Refresh *****/
if (get_option('lp_opt_cache_actions_enabled') == '1') {
    $ret = include_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'FileCache.php');
    if ($ret) {
        $lp_cache_args = get_option('lp_opt_cache_args');
        if (!is_array($lp_cache_args)) {
            if (is_string($lp_cache_args) && !empty($lp_cache_args))
                $lp_cache_args = @unserialize($lp_cache_args);
            if (!is_array($lp_cache_args)) {
                trigger_error('non-array cache arguments in lightpress.php ' . __LINE__ . ' ' . var_export($lp_cache_args, true), E_USER_WARNING);
                $lp_cache_args = array();
            }
        } else {
            $lp_cache_args['dir'] = stripslashes($lp_cache_args['dir']);
        }
        $lp_cache =& new LightPressCache($lp_cache_args);
        if (get_option('lp_opt_cache_actions_all_pages') == '1') {
            add_action('publish_post', array(&$lp_cache, 'clearCache'), 99);
            //add_action('edit_post', array(&$lp_cache, 'clearCache'), 99);
            add_action('comment_post', array(&$lp_cache, 'clearCache'), 99);
            add_action('edit_comment', array(&$lp_cache, 'clearCache'), 99);
            add_action('delete_comment', array(&$lp_cache, 'clearCache'), 99);
        } else {
            add_action('publish_post', array(&$lp_cache, 'clearCachePost'), 99);
            //add_action('edit_post', array(&$lp_cache, 'clearCachePost'), 99);
            add_action('comment_post', array(&$lp_cache, 'clearCacheComment'), 99);
            add_action('edit_comment', array(&$lp_cache, 'clearCacheComment'), 99);
            add_action('delete_comment', array(&$lp_cache, 'clearCacheComment'), 99);
        }
    }
}

if (get_option('lp_opt_use_preformatter') == '1') {
    $ret = include_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'LightpressPreformatter.php');
    if ($ret) {
        $lp_preformatter_method = get_option('lp_opt_preformatter_method');
        $lp_preformatter =& new LightpressPreformatter($lp_preformatter_method);
        $lp_preformatter->register_filters();
    }
}

?>
